#ifndef __YB_FOC_H__
#define __YB_FOC_H__

#include "main.h"

//==============================================================================
extern uint16_t   	pwm_period;
 
extern uint16_t   	pwm_duty_a;
extern uint16_t   	pwm_duty_b;
extern uint16_t   	pwm_duty_c;
 
extern uint16_t   	vadc_ia, vadc_ib, vadc_ic, vadc_vpot;

extern uint32_t	    adc_isr_flag, opa_cali_flag;
//==============================================================================
extern uint16_t	    ia_offset, ib_offset, ic_offset;
extern int16_t		ia_mea, ib_mea, ic_mea;
 
extern float		ia_mea_f, ib_mea_f, ic_mea_f;
 
extern float		Ialpha, Ibeta;
extern float		Id_mea, Iq_mea;
 
extern float		Valpha;   
extern float		Vbeta;
 
extern volatile float Vd ;   
extern volatile float Vq;   
 
extern float		theta;
extern float		theta_inc;
extern float		theta_est;
//---------------------------------------------------------------------
extern float		Id_ref;
extern float		Id_err;
extern float		Kp_d;	
extern float		Ki_d;	
extern float		Kc_d;	
extern float		sum_d; 

extern float		Iq_ref;
extern float		Iq_err;
extern float		Kp_q;	
extern float		Ki_q;
extern float		Kc_q;		
extern float		sum_q; 

extern float		sp_err;
extern float		KcErr_sp;	
extern float		Kp_sp;	
extern float		Ki_sp;	
extern float		Kc_sp;	
extern float		sum_sp; 
extern float		omega_val;
//---------------------------------------------------------------------
extern float		Ed;
extern float		Eq; 
extern float        e_omega_set;
extern float		e_omega_est;
extern float        e_omega_flt;
extern float 		sum_omega;
extern float 		lv_coe;

extern float   	    R;  			//
extern float		L; 		//
extern float		Kp_pll;				
extern float		Ki_pll;					
extern float		sum_pll;
extern float		theta_err;
extern float		theta_err_limit;		//
//---------------------------------------------------------------------
extern float		Va_abs, Vb3_abs;
extern float		d1, d2, d3, d4, d5, d6, d7;
extern float		da, db, dc;
extern float		Vbat;
extern float		K_svpwm;	//
extern float		two_sqrt3;
extern float		one_sqrt3;
//---------------------------------------------------------------------
extern volatile	uint16_t	mode;
extern uint16_t	delay_cnt;
extern uint16_t	ramp_cnt;
extern uint16_t	ramp_val;
extern volatile	uint16_t	sw1_flag;
extern uint32_t	chk_cnt;
extern uint16_t	led_cnt;
extern uint16_t	led_flag;

void dq_cicle_limit(void);
void d_axis_current_loop (void);
void q_axis_current_loop (void);
void speed_loop (void);
void open_2_close_cal(void);

void position_estimate(void);


#endif


